package com.example.wordserver;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableField;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;

public class codeAutoGenerate {
    public static void main(String[] args) {
        //构建代码自动生成器
        AutoGenerator autoGenerator = new AutoGenerator();
        //配置策略
        //1.全局配置,一定是mybatis-generator下面的包
        GlobalConfig gc= new GlobalConfig();
        //返回当前工作路径
        String projectPath = System.getProperty("user.dir");
        //生成的代码输出路径
        gc.setOutputDir(projectPath+"/src/main/java");
        //
        gc.setAuthor("lhx");
        gc.setOpen(false);
        gc.setFileOverride(false);
        gc.setServiceName("%sService");//是否去掉service的I前缀
        gc.setIdType(IdType.AUTO);//Id的生成类型
        gc.setDateType(DateType.ONLY_DATE);
        gc.setSwagger2(true);


        autoGenerator.setGlobalConfig(gc);
        //设置数据源
        DataSourceConfig dataSourceConfig=new DataSourceConfig();

        dataSourceConfig.setUrl("jdbc:mysql://localhost:3306/world?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true");
        dataSourceConfig.setPassword("123456");
        dataSourceConfig.setUsername("root");
        dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");

        autoGenerator.setDataSource(dataSourceConfig);
        //配置包
//        autoGenerat
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setParent("com.example.wordserver");
//        packageConfig.setModuleName("bank");
        packageConfig.setEntity("entity");
        packageConfig.setController("controller");
        packageConfig.setMapper("Mapper");
        packageConfig.setService("Service");
        autoGenerator.setPackageInfo(packageConfig);

        //策略
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig.setInclude("country","countrylanguage");//生成哪张表
        strategyConfig.setNaming(NamingStrategy.underline_to_camel);
        strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
        strategyConfig.setEntityLombokModel(true);//是否开k;
//        逻辑删除
        strategyConfig.setLogicDeleteFieldName("deleted");
//        自动填充
        TableFill createTime = new TableFill("gmt_create", FieldFill.INSERT);
        TableFill modifiedTime = new TableFill("gmt_modified", FieldFill.INSERT_UPDATE);
        ArrayList<TableFill> tableFills = new ArrayList<>();
        tableFills.add(createTime);
        tableFills.add(modifiedTime);
        strategyConfig.setTableFillList(tableFills);
        //乐观锁
        strategyConfig.setVersionFieldName("version");
        strategyConfig.setRestControllerStyle(true);//开启驼峰命名
        strategyConfig.setControllerMappingHyphenStyle(true);
        autoGenerator.setStrategy(strategyConfig);

        autoGenerator.execute();


    }
}
